package com.todod.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ApprovalCommentsDto;
import com.todod.mapper.ApprovalCommentsMapper;
import com.todod.mapper.ProjectFileMapper;
import com.todod.mapper.PublicFileMapper;
import com.todod.model.ApprovalComments;
import com.todod.model.ProjectFile;
import com.todod.model.PublicFile;

@Service
@Transactional
public class ApprovalCommentsService {

	@Autowired
	private ApprovalCommentsMapper approvalCommentsMapper;
	@Autowired
	private ProjectFileMapper projectFileMapper;
	@Autowired
	private PublicFileMapper publicFileMapper;

	/**
	 * 
	 * @param _userId
	 * @param otherId
	 * @param approvalStatus
	 * @param flag
	 */
	public void saveData(Long _userId, Long otherId, String comments, String approvalStatus, String flag) {
		ApprovalComments approvalComments = new ApprovalComments();
		approvalComments.setId(SnowflakeIdGenerator.getId());
		approvalComments.setOtherId(otherId);
		approvalComments.setComments(comments);
		approvalComments.setStatus(approvalStatus);

		approvalComments.setCreatetimeTi(LocalDateTime.now());
		approvalComments.setCreateuserId(_userId);
		approvalCommentsMapper.insert(approvalComments);

		if (flag.equalsIgnoreCase("XF")) {
			ProjectFile projectFile = projectFileMapper.selectById(otherId);
//			projectFile.setApprovelState(approvalStatus);
			projectFile.setModifyuserId(_userId);
			projectFile.setModifytimeTi(LocalDateTime.now());
			projectFileMapper.updateById(projectFile);
		} else if (flag.equalsIgnoreCase("GF")) {
			PublicFile publicFile = publicFileMapper.selectById(otherId);
			publicFile.setApprovelState(approvalStatus);
			publicFile.setModifyuserId(_userId);
			publicFile.setModifytimeTi(LocalDateTime.now());
			publicFileMapper.updateById(publicFile);
		}
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public Object getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		ApprovalCommentsDto approvalComments = approvalCommentsMapper.getInfo(code);
		map.put("approvalComments", approvalComments);
		return map;
	}

}
